<script setup lang="ts">
import { Api } from '@/assets/apis';
import { ref, onMounted } from 'vue'
import type { HomeDataType } from '@/typings'
import { Toast } from 'vant';
import { useRouter } from 'vue-router';


const homeData = ref<Array<HomeDataType>>([])
const loading = ref(false)
const router = useRouter()

const book: [{ title: string, author: string, coverImage: string, id: string }] = JSON.parse(localStorage.getItem('bookshelf') as string) || []

const detail = (id: string, title: string) => {

    router.push(`/book/${id}?title=${title}`)
}

onMounted(async () => {
    Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
    });
    let { data } = await Api.getFemale()

    homeData.value = data.data.list
    console.log(data);

    Toast.clear()
    loading.value = true
})
</script>

<template>
    <div v-show="loading">
        <div class="history" @click="router.push('/bookshelf')">
            <div class="last-book">
                <van-icon name="clock-o" />
                {{ book[0]?.title }}
            </div>
            <div class="book">
                书架
                <van-icon name="arrow" />
            </div>
        </div>
        <div class="list">
            <div class="title">
                <div class="title-name">重磅推荐</div>
                <div class="more">
                    更多
                </div>
            </div>
            <div class="book-list">
                <div class="item" v-for="a in homeData[1]?.list" @click="detail(a.id, a.title)">
                    <div class="poster">
                        <img :src="a.cover" alt="">
                    </div>
                    <div class="name">{{ a.title }}</div>
                    <div class="author">{{ a.author }}</div>
                </div>
            </div>
            <div class="more-list">
                <div class="more-item" v-for="b in homeData[2]?.list" @click="detail(b.id, b.title)">
                    <div class="more-poster">
                        <img :src="b.cover" alt="">
                    </div>
                    <div class="more-content">
                        <div class="content-name">{{ b.title }}</div>
                        <div class="content-author">{{ b.author }}</div>
                        <div class="content-desc">{{ b.content }}</div>
                        <div class="tags">
                            <div>连载</div>
                            <div>{{ b.category }}</div>
                            <div>141.95万字</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="list">
            <div class="title">
                <div class="title-name">都市爽文</div>
                <div class="more">
                    更多
                </div>
            </div>
            <div class="more-list">
                <div class="more-item" v-for="c in homeData[3]?.list" @click="detail(c.id, c.title)">
                    <div class="more-poster">
                        <img :src="c.cover" alt="">
                    </div>
                    <div class="more-content">
                        <div class="content-name">{{ c.title }}</div>
                        <div class="content-author">{{ c.author }}</div>
                        <div class="content-desc">{{ c.content }}</div>
                        <div class="tags">
                            <div>连载</div>
                            <div>{{ c.category }}</div>
                            <div>141.95万字</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="list">
            <div class="title">
                <div class="title-name">现代言情</div>
                <div class="more">
                    更多
                </div>
            </div>
            <div class="more-list">
                <div class="more-item" v-for="d in homeData[4]?.list" @click="detail(d.id, d.title)">
                    <div class="more-poster">
                        <img :src="d.cover" alt="">
                    </div>
                    <div class="more-content">
                        <div class="content-name">{{ d.title }}</div>
                        <div class="content-author">{{ d.author }}</div>
                        <div class="content-desc">{{ d.content }}</div>
                        <div class="tags">
                            <div>连载</div>
                            <div>{{ d.category }}</div>
                            <div>141.95万字</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="list">
            <div class="title">
                <div class="title-name">最新红书</div>
                <div class="more">
                    更多
                </div>
            </div>
            <div class="more-list">
                <div class="more-item" v-for="e in homeData[5]?.list" @click="detail(e.id, e.title)">
                    <div class="more-poster">
                        <img :src="e.cover" alt="">
                    </div>
                    <div class="more-content">
                        <div class="content-name">{{ e.title }}</div>
                        <div class="content-author">{{ e.author }}</div>
                        <div class="content-desc">{{ e.content }}</div>
                        <div class="tags">
                            <div>连载</div>
                            <div>{{ e.category }}</div>
                            <div>141.95万字</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="list">
            <div class="title">
                <div class="title-name">完本精选</div>
                <div class="more">
                    更多
                </div>
            </div>
            <div class="more-list">
                <div class="more-item" v-for="f in homeData[6]?.list" @click="detail(f.id, f.title)">
                    <div class="more-poster">
                        <img :src="f.cover" alt="">
                    </div>
                    <div class="more-content">
                        <div class="content-name">{{ f.title }}</div>
                        <div class="content-author">{{ f.author }}</div>
                        <div class="content-desc">{{ f.content }}</div>
                        <div class="tags">
                            <div>完结</div>
                            <div>{{ f.category }}</div>
                            <div>141.95万字</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.history {
    width: 335rem;
    height: 56rem;
    margin: 0 20rem;
    display: flex;
    justify-content: space-between;
    line-height: 56rem;
    text-align: center;
    font-size: 15rem;
    color: #595550;
    border-bottom: 1rem solid #ededed;
}

.list {
    width: 335rem;
    // height: 656rem;
    padding: 15rem 0 30rem 0;
    margin: 0 20rem 4rem 20rem;
    border-bottom: 1rem solid #ededed;

    .title {
        width: 335rem;
        height: 28rem;
        margin-bottom: 16rem;
        display: flex;
        justify-content: space-between;
        line-height: 28rem;

        .title-name {
            font-size: 20rem;
            font-weight: bold;
            color: #1a1a1a;
        }

        .more {
            color: #999;
            font-size: 13rem;
            border: 1rem solid #f0f0f0;
            padding: 0 17rem;
            border-radius: 28rem;
        }
    }

    .book-list {
        width: 335rem;
        overflow: hidden;
        // height: 219rem;
        display: flex;

        .item {
            width: 93rem;
            height: 200rem;
            margin: 0 26rem 20rem 0;

            .poster {
                width: 93rem;
                height: 131rem;

                img {
                    width: 100%;
                    height: 100%;
                    border-radius: 5rem;
                }
            }

            .name {
                width: 93rem;
                height: 38rem;
                margin: 10rem 0 5rem 0;
                font-weight: bold;
                font-size: 15rem;
                line-height: 19rem;
                color: #222;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
            }

            .author {
                font-size: 12rem;
                color: #888;
            }
        }
    }

    .more-list {
        width: 335rem;
        margin-top: 8rem;

        .more-item {
            display: flex;
            margin-bottom: 20rem;

            .more-poster {
                width: 82rem;
                height: 115rem;
                margin-right: 15rem;

                img {
                    width: 100%;
                    height: 100%;
                    border-radius: 5rem;
                }
            }

            .more-content {
                width: 238rem;

                .content-name {
                    height: 20rem;
                    margin-bottom: 7rem;
                    font-size: 17rem;
                    color: #222;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                }

                .content-author {
                    color: #999;
                    height: 14rem;
                    margin-bottom: 9rem;
                }

                .content-desc {
                    height: 36rem;
                    line-height: 18rem;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    font-size: 12rem;
                    color: #999;
                }

                .tags {
                    height: 22rem;
                    line-height: 22rem;
                    margin-top: 5rem;
                    display: flex;

                    div {
                        height: 22rem;
                        color: #999;
                        padding: 0 12rem;
                        margin-right: 2rem;
                        background-image: linear-gradient(3deg, #F9F9F9 0, #F0F0F0 100%);
                        border-radius: 22rem;
                        font-size: 12rem;
                    }
                }
            }
        }
    }
}
</style>